<!--
 The BSD 3-Clause License

 Copyright 2022 - DATATRONiQ GmbH (https://datatroniq.com)
 Copyright (c) 2018-2022 Klaus Landsdorf (http://node-red.plus/)
 Copyright 2015,2016 - Mika Karaila, Valmet Automation Inc. (node-red-contrib-opcua)
 All rights reserved.
 node-red-contrib-iiot-opcua
-->

<script type="text/javascript">
  RED.nodes.registerType('OPCUA-IIoT-Write', {
    category: 'IIoT',
    color: '#ABCDEF',
    defaults: {
      connector: {type: "OPCUA-IIoT-Connector", required: true},
      name: {value: ""},
      justValue: {value: true},
      showStatusActivities: {value: false},
      showErrors: {value: false}
    },
    inputs: 1,
    outputs: 1,
    align: "right",
    icon: "icon.png",
    label: function () {
      return this.name || "Write";
    },
    labelStyle: function () {
      return this.name ? "node_label_italic" : "";
    }
  });
</script>

<script type="text/x-red" data-template-name="OPCUA-IIoT-Write">
    <div class="form-row" style="min-width:640px">
        <label for="node-input-connector"><i class="icon-globe"></i> <span data-i18n="opcua-iiot-contrib.label.connector"></span></label>
        <input type="text" id="node-input-connector">
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-justValue"><i class="fa fa-simplybuilt"></i>
        <span data-i18n="opcua-iiot-contrib.label.justValue"></span></label>
        <input type="checkbox" id="node-input-justValue" style="max-width:30px">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-showStatusActivities"><i class="fa fa-bolt"></i>
        <span data-i18n="opcua-iiot-contrib.label.showActivities"></span></label>
        <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
    </div>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-showErrors"><i class="fa fa-exclamation-circle"></i>
        <span data-i18n="opcua-iiot-contrib.label.showErrors"></span></label>
        <input type="checkbox" id="node-input-showErrors" style="max-width:30px">
    </div>
</script>

<script type="text/x-red" data-help-name="OPCUA-IIoT-Write">
    <h2>OPC UA IIoT Write</h2>

    <p>The Write node is for sending data to the OPC UA server.
    It handles a single data request and multiple data requests.
    All write requests will produce an array of StatusCodes for writing
    in amount as applied for and responded.</p>

    <p>It needs a <pre>msg.valuesToWrite</pre> to write values and datatypeName in the addressSpaceItems objects is required.</p>

    <p>Every entry in the addressSpaceItems corresponding to each array place in msg.valuesToWrite.
    That means addressSpaceItem[0] will write the data from msg.valuesToWrite[0]!</p>

    <p>It is important to have the injectType write in the msg object!
    That is to protect your flow in event based flows.</p>


    <h3>Input</h3>

    <ul>
      <li>payload (value to write one or all)</li>
      <li>topic</li>
      <li>addressSpaceItems or nodesToWrite (Array of Node-Ids)
      <li>nodetype (inject)</li>
      <li>injectType (write)
      <li>valuesToWrite (Array of values or objects to write individual)</li>
    </ul>

    <h3>Output</h3>

    <p>Results in message:</p>
    <ul>
      <li>payload</li>
      <ul>
        <li>statusCodes (Array of Status Codes)
        <li>nodesToWrite (Array of Node-Id)</li>
      </ul>
      <li>topic
      <li>nodetype (write)</li>
      <li>resultsConverted (Array)
    </ul>

    <p>Set showErrors to get errors from node-opcua on browse.</p>
</script>
